home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xless / xless141.z / xless141 / xless-1.4.1 / window.c < prev    next >
C/C++ Source or Header  |  1993-05-05  |  5KB  |  240 lines

  1. /*
  2.  * $Header: /usr/sww/share/src/X11R5/local/clients/xless-1.4/RCS/window.c,v 1.11 1993/03/02 02:07:05 dglo Exp dglo $
  3.  */
  4.  
  5. #include "xless.h"
  6. #include "XLessWin.icon"
  7.  
  8. /* list of all xless windows */
  9. WindowInfo *windowlist = 0;
  10. int windowcount = 0;
  11.  
  12. /* flag for "too many windows" popup */
  13. static int tooManyFlag = 0;
  14.  
  15. static WindowInfo *
  16. findWindowInfo(w)
  17. Widget w;
  18. {
  19.   extern WindowInfo *windowlist;
  20.   WindowInfo *wi = windowlist;
  21.  
  22.   while (w) {
  23.  
  24.     /* if this is the shell widget... */
  25.     if (XtClass(w) == applicationShellWidgetClass) {
  26.  
  27.       /* find this window in the list */
  28.       while (wi && (wi->base != w))
  29.         wi = wi->next;
  30.       return(wi);
  31.     }
  32.  
  33.     /* see if parent is the shell widget */
  34.     w = XtParent(w);
  35.   }
  36.  
  37.   /* couldn't find a window associated with this widget */
  38.   return(0);
  39. }
  40.  
  41. static void
  42. popdownTooMany(widget, closure, callData)
  43. Widget widget;
  44. XtPointer closure;
  45. XtPointer callData;
  46. {
  47.   XtPopdown((Widget)closure);
  48.   tooManyFlag = 0;
  49. }
  50.  
  51. int
  52. CreateWindow(top, filename)
  53. Widget top;
  54. const char *filename;
  55. {
  56.   FILE *file;
  57.   Cardinal i;
  58.   Arg args[2];
  59.   const char *memory;
  60.   Pixmap windowicon;
  61.   Widget new, main_frame;
  62.   WindowInfo *wi;
  63.   XLessFlag flag = XLessClearFlag;
  64.   const char *fixedname;
  65.   static Widget badFileMsg = 0;
  66.   static Widget memErrMsg = 0;
  67.  
  68.   /* make sure we haven't created *too* many windows */
  69.   if (resources.maxWindows && (windowcount >= resources.maxWindows)) {
  70.     static Widget tooManyMsg = 0;
  71.     char message[64];
  72.  
  73.     if (!tooManyFlag) {
  74.       tooManyFlag = 1;
  75.       sprintf(message, "Can't have more than %d windows!",
  76.           resources.maxWindows);
  77.       if (!tooManyMsg)
  78.     tooManyMsg = MessageBox(top, message, "OK", popdownTooMany, 0);
  79.       if (tooManyMsg)
  80.     SetPopup(top, tooManyMsg);
  81.     }
  82.     return 1;
  83.   }
  84.  
  85.   /* get a filehandle (or exit) */
  86.   if (filename) {
  87.  
  88. #ifdef TILDE_EXPANSION
  89.     /* see if we need to do tilde expansion */
  90.     if (filename && *filename == '~') {
  91.       filename = TildeExpand(filename);
  92.       if (*filename != '~')
  93.     flag |= XLessFreeFilename;
  94.     }
  95. #endif /* TILDE_EXPANSION */
  96.  
  97.     /* try to open the file */
  98.     if ((file = fopen(filename, "r")) == NULL) {
  99.       CouldntOpen(top, filename);
  100.       return 2;
  101.     }
  102.  
  103.     fixedname = filename;
  104.  
  105.   } else if (windowcount == 0) {
  106.  
  107.     /* first file can come from stdin */
  108.     fixedname = "stdin";
  109.     file = stdin;
  110.  
  111.   } else {
  112.  
  113.     /* don't let 'em get away with this!!! */
  114.     if (!badFileMsg)
  115.       badFileMsg = MessageBox(top, "Please specify a file name!",
  116.                   "OK", 0, 0);
  117.     if (badFileMsg)
  118.       SetPopup(top, badFileMsg);
  119.     return 3;
  120.  
  121.   }
  122.  
  123.   /* keep track of the new window */
  124.   if ((wi = (WindowInfo *) malloc(sizeof(WindowInfo))) == NULL){
  125.     if (!memErrMsg)
  126.       memErrMsg= MessageBox(top,
  127.                 "Couldn't save internal window information!",
  128.                 "OK", 0, 0);
  129.     if (memErrMsg)
  130.       SetPopup(top, memErrMsg);
  131.     return 4;
  132.   }
  133.   wi->file = fixedname;
  134.   wi->flag = flag;
  135.  
  136.   /* read the file into memory */
  137.   memory = InitData(file);
  138.   wi->memory = memory;
  139.  
  140.   /* create a new application shell */
  141.   i = 0;
  142.   windowicon = XCreateBitmapFromData(disp, XRootWindow(disp,0),
  143.                      XLessWin_bits, XLessWin_width,
  144.                      XLessWin_height);
  145.   XtSetArg(args[i], XtNiconPixmap, windowicon); i++;
  146.   XtSetArg(args[i], XtNallowShellResize, TRUE); i++;
  147.   new = XtAppCreateShell("new", XLESS_CLASS, applicationShellWidgetClass,
  148.              disp, args, i);
  149.  
  150.   /* set icon & title name for new window */
  151.   SetXNames(new, fixedname);
  152.  
  153.   /* create the container for the subwindows */
  154. #ifdef FRAME_IS_FORM
  155.   i = 0;
  156.   main_frame = XtCreateManagedWidget("frame", formWidgetClass, new, args, i);
  157. #else
  158.   i = 0;
  159.   XtSetArg(args[i], XtNorientation, XtorientHorizontal); i++;
  160.   main_frame = XtCreateManagedWidget("frame", panedWidgetClass, new, args, i);
  161. #endif
  162.  
  163.   /* build widgets for new window */
  164.   wi->base = new;
  165.   wi->text = MakeText(main_frame, memory);
  166.   wi->toolbox = MakeToolbox(main_frame, wi, filename);
  167.  
  168.   /* make sure text window gets all keystrokes */
  169.   XtSetKeyboardFocus(main_frame, wi->text);
  170.  
  171.   /* no dialog boxes yet */
  172.   wi->search_popup = 0;
  173.   wi->newwin_popup = 0;
  174.   wi->chgfile_popup = 0;
  175.  
  176.   /* display the window */
  177.   XtPopup(new, XtGrabNone);
  178.  
  179.   /* add this window to the list */
  180.   wi->next = windowlist;
  181.   windowlist = wi;
  182.   windowcount++;
  183.  
  184.   SetWMHints(wi);
  185.  
  186.   return 0;
  187. }
  188.  
  189. void
  190. DestroyWindowInfo(wi)
  191. WindowInfo *wi;
  192. {
  193.   WindowInfo *prev;
  194.  
  195.   /* free all window-related widgets */
  196.   XtDestroyWidget(wi->base);
  197.  
  198.   /* free all existing dialog boxes */
  199.   if (wi->search_popup)
  200.     XtDestroyWidget(wi->search_popup);
  201.   if (wi->newwin_popup)
  202.     XtDestroyWidget(wi->newwin_popup);
  203.   if (wi->chgfile_popup)
  204.     XtDestroyWidget(wi->chgfile_popup);
  205.  
  206.   /* free text memory */
  207.   free(wi->memory);
  208.  
  209.   /* free filename string if it was malloc'd */
  210.   if (wi->flag & XLessFreeFilename)
  211.     free(wi->file);
  212.  
  213.   /* remove from windowlist chain */
  214.   if (windowlist == wi)
  215.     windowlist = wi->next;
  216.   else {
  217.     prev = windowlist;
  218.     while (prev && (prev->next != wi))
  219.       prev = prev->next;
  220.     if (prev)
  221.       prev->next = wi->next;
  222.   }
  223.  
  224.   /* one less window to worry about... */
  225.   free(wi);
  226.   windowcount--;
  227.  
  228.   /* nothing else to do if we've closed all the windows */
  229.   if (windowcount == 0) {
  230.     exit(0);
  231.   }
  232. }
  233.  
  234. void
  235. DestroyAllWindows()
  236. {
  237.   while (windowlist)
  238.     DestroyWindowInfo(windowlist);
  239. }
  240.